# 创建实体草稿,token可能会过期，最好每次更新一下

import pandas as pd
from dao.mysqlhelper import MySQLHelper
from base.base import Base
import json

# 读取Excel文件，文件里的值最好都设置成文本
file_path = 'D:\\IOT3.0\\2024.09\\实体赋码平台\\导入实体1005ceshi.xlsx'  # 替换为你的文件路径
df = pd.read_excel(file_path, engine='openpyxl')

# 遍历每一行数据
for index, row in df.iterrows():
    mysql=MySQLHelper()
    sql1=f"select category_id from entity_category where node_name ='{row['实体分类']}'"
    categoryId=mysql.select(sql1)[0][0]

    address=row['标准地址'].replace('/','')
    sql2=f"select district_code from aux_district where std_address ='{address}'"
    divisionCode=mysql.select(sql2)[0][0]

    sql3=f"select model_vid from entity_model_version where category_id ='{categoryId}'"
    modelVid=mysql.select(sql3)[0][0]

    base=Base()
    url='http://192.168.143.99:10074/gateway/code-one/entityInfo/saveDraft'
    param_type='application/json'
    headers={
        'content-type':'application/json',
        'authorization':'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiIxODM4NTE3NDc2ODU5MTcwODE3Iiwic3ViIjoienpoIiwiYXVkIjoidGVuYW50IiwidW5hbWUiOiJ6emgiLCJhcHBJZCI6ImRlZmF1bHRBcHBJZCIsImlzcyI6ImlkYWFzIiwidGVuYW50SWQiOiJkZWZhdWx0VGVuYW50SWQiLCJ1dHlwZSI6InVzZXIiLCJybmQiOiIxN2EzazNrdyIsImV4cCI6MTcyODE3MjYxMSwiaWF0IjoxNzI4MDg2MjExfQ.VJuLVuvRSJPS4Q1MhIdPuuQzspugtNo2YgUiwSj6Z9U'
    }
    data={
        "categoryId": categoryId,
        "entityName": row['实体名称'],
        "stdAddress": row['标准地址'],
        "detailAddress": row['详细地址'],
        "belongingSpace": "",
        "longitude": row['经度'],
        "latitude": row['纬度'],
        "manageUnit": "1",
        "altitude": row['大地高'],
        "belongUnit": "1838100888007725058",
        "remark": row['描述'],
        "extendAttributes": "",
        "entityRelateVoList": [],
        "divisionCode": divisionCode,
        "modelVid": modelVid
    }

    try:
        res=json.loads(base.send_post(url,param_type,headers=headers,data=data))

        # 检查请求是否成功
        if res['msg'] == '保存成功':
            print(f"成功提交第 {index + 1} 行数据: {data}")
        else:
            print(f"第 {index + 1} 行数据提交失败，状态码: {res.status_code}, 响应: {res.text}")

    except Exception as e:
        print(f"调用接口时发生错误: {e}")